package com.pac.fragment;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.pac.MyApp;
import com.pac.R;
import com.pac.adapter.MyGridAdapter;
import com.pac.adapter.ViewPagerAdapter;
import com.pac.bean.homepage.Home;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link FM_Home.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link FM_Home#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FM_Home extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private OnFragmentInteractionListener mListener;
    private Home homedata;
    private GridView gridView_class;
    private GridView gridView_star;
    private ViewPager viewPager_home;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FM_Home.
     */
    // TODO: Rename and change types and number of parameters
    public static FM_Home newInstance(String param1, String param2) {
        FM_Home fragment = new FM_Home();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public FM_Home() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.fragment_home, container, false);
        MyApp.httpUtils.send(HttpRequest.HttpMethod.GET,mParam1,new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> objectResponseInfo) {
                homedata=Home.getInstanceFromJsonStr(objectResponseInfo.result);
                gridView_class = (GridView) v.findViewById(R.id.home_gridview_class);
                gridView_star = (GridView) v.findViewById(R.id.home_gridview_star);
                viewPager_home = (ViewPager) v.findViewById(R.id.home_viewpager);
                viewPager_home.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        RadioGroup ra = (RadioGroup) v.findViewById(R.id.topview_radiogroup);
                        RadioButton button = (RadioButton) ra.getChildAt(position);
                        button.setChecked(true);
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
                //////////////////viewpager
                ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity(),homedata.getSlide());
                //////////////////gridview
                MyGridAdapter gridview_star_adapter = new MyGridAdapter(homedata,getActivity(),1);
                MyGridAdapter gridview_class_adapter = new MyGridAdapter(homedata,getActivity(),0);
//                /////////////////数据更新
//                gridview_class_adapter.notifyDataSetChanged();
//                gridview_star_adapter.notifyDataSetChanged();
//                adapter.notifyDataSetChanged();
                viewPager_home.setAdapter(adapter);
                gridView_class.setAdapter(gridview_class_adapter);
                gridView_star.setAdapter(gridview_star_adapter);

                /////////////////点击事件,用不着了
//                gridView_class.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                        Toast.makeText(getActivity(),"onclick",Toast.LENGTH_SHORT).show();
//                        Uri uri = Uri.parse("http://www.baidu.com");
//                        Intent it = new Intent(Intent.ACTION_VIEW, uri);
//                        startActivity(it);
////                        WebView.loadUrl("http://www.baidu.com");
//                    }
//                });
//                gridView_star.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//                    }
//                });

                /**
                 * 尾部视图
                 */

                class Holder{
                    TextView text;
                    ImageView image1;
                    ImageView image2;
                    ImageView image3;
                    ImageView image4;
                }
                Holder holder=new Holder();
                holder.text=(TextView) v.findViewById(R.id.home_bottom_item_text);
                holder.image1= (ImageView) v.findViewById(R.id.home_bottom_item_image1);
                holder.image2= (ImageView) v.findViewById(R.id.home_bottom_item_image2);
                holder.image3= (ImageView) v.findViewById(R.id.home_bottom_item_image3);
                holder.image4= (ImageView) v.findViewById(R.id.home_bottom_item_image4);
                ImageView images[]={holder.image1,holder.image2,holder.image3,holder.image4};
                for (int i=0;i<homedata.getGcate().size();i++) {
                    holder.text.setText(homedata.getGcate().get(i).getCate_name());
                    for (int j=0;j<homedata.getGcate().get(i).getPic().size();j++){
                        ViewGroup.LayoutParams params=images[i].getLayoutParams();
                        params.height=100;
                        params.width=100;
                        images[i].setLayoutParams(params);
                        MyApp.myImageXutils.display(images[j],homedata.getGcate().get(i).getPic().get(j));
                    }
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });

        return v;
    }
    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }
    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }
}
